package net.nshc.kwkim.mycalc.buttonlistener;

import net.nshc.kwkim.mycalc.util.*;
import android.content.Context;
import android.view.View;

public class BackButtonClickListener extends ButtonClickListener {

	public BackButtonClickListener(Context context) {
		super(context);
	}

	@Override
	public void onClick(View v) {
		StringBuilder diaplayContentToBeUpdated
		= new StringBuilder(ButtonContentAnalyzer.hasStingErrorMessage(editTextInput.getText().toString()));

		int contentLength = diaplayContentToBeUpdated.length();
		int deleteCount = getDeleteCount(diaplayContentToBeUpdated.toString());

		if (contentLength >= 1) {
			diaplayContentToBeUpdated.delete(contentLength - deleteCount, contentLength);
		}
		editTextInput.setText(diaplayContentToBeUpdated.toString());
	}

	private int getDeleteCount(String strContent){
		String strTemp = new String();
		strTemp = EqualSignButtonClickListener.changeExtendexp(strContent);
		int retValue=0;
		if(strTemp.length() >= 1){
			if(strTemp.substring(strTemp.length()-1,strTemp.length()).equals("t")){

				retValue = 3;

			}else if(strTemp.substring(strTemp.length()-1,strTemp.length()).equals("c")){

				retValue = 3;

			}else if(strTemp.substring(strTemp.length()-1,strTemp.length()).equals("s")){

				retValue = 3;

			}else if(strTemp.substring(strTemp.length()-1,strTemp.length()).equals("l")){

				retValue = 3;

			}else if(strTemp.substring(strTemp.length()-1,strTemp.length()).equals("f")){

				retValue = 3;

			}else if(strTemp.substring(strTemp.length()-1,strTemp.length()).equals("n")){

				retValue = 2;

			}else if(strTemp.substring(strTemp.length()-1,strTemp.length()).equals("x")){

				retValue = 3;

			}else if(strTemp.substring(strTemp.length()-1,strTemp.length()).equals("b")){

				retValue = 3;

			}else if(strTemp.substring(strTemp.length()-1,strTemp.length()).equals("p")){

				retValue = 3;

			}else if(strTemp.substring(strTemp.length()-1,strTemp.length()).equals("q")){

				retValue = 4;

			}else{
				retValue = 1;
			}
		}



		return retValue;
	}

}